package com.phapli.versionchecking.Adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.phapli.versionchecking.R;
import com.phapli.versionchecking.object.Item;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by D.PHI on 3/7/2015.
 */
public class ItemAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<Item> list_item;

    public ItemAdapter ( Context context, ArrayList<Item> list_item)
    {
        this.context = context;
        this.list_item = list_item;
    }



    @Override
    public int getCount() {
        return list_item.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        final View rowView = inflater.inflate(R.layout.rowlayout, null, false);

        TextView tv_type_of_version = (TextView) rowView.findViewById(R.id.tv_type_of_version);
        TextView tv_version = (TextView) rowView.findViewById(R.id.tv_version);
        TextView tv_time = (TextView) rowView.findViewById(R.id.tv_time);
        Button btn_update_if_need = (Button) rowView.findViewById(R.id.btn_update_if_need);


        if (position == 0 )
        {
            tv_type_of_version.setText("Version Installed");
            btn_update_if_need.setVisibility(View.GONE);
        }
        else
        {
            tv_type_of_version.setText(list_item.get(position).siteName);
        }
        tv_version.setText(list_item.get(position).version);
        tv_time.setText("Time to check");
        btn_update_if_need.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_VIEW,
                        Uri.parse(list_item.get(position).url));
                context.startActivity(i);
            }
        });

        SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM , yyyy");
        String date = df.format(new Date());
        tv_time.setText(date);


        return rowView;
    }
}
